﻿// ===============================================================================
// Project Name        :    ResourcesFile4DotNet
// Project Description :   
// ===============================================================================
// Class Name          :    ResourcesFile4DotNetFrom
// Class Version       :    v1.0.0.1
// Class Description   :    资源文件处理程序的WinForm版本，比较直观地进行操作
// Author              :    HUANGKX 小飞兔
// Create Time         :    2018/1/19 9:20:15
// Update Time         :    2018/1/19 16:45:15
// ===============================================================================
// Copyright 2018 HUANGKX 小飞兔 . 
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ===============================================================================

using System;
using System.Drawing;
using System.IO;
using System.Resources;
using System.Windows.Forms;

namespace ResourcesFile4DotNetFrom
{
    public partial class ResourcesFile4DotNetFrom : Form
    {
        public ResourcesFile4DotNetFrom()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string sResourcesFileName = txtResourcesFileName.Text;
            string sImagePath = txtImagePath.Text;

            try
            {
                //Resources资源文件路径
                ResourceWriter rw = new ResourceWriter(sResourcesFileName);
                
                DirectoryInfo dirInfo = new DirectoryInfo(sImagePath);
                //以png为例，也可以添加其他图片
                FileInfo[] files = dirInfo.GetFiles("*.png");
                foreach (FileInfo file in files)
                {
                    Image img = Image.FromFile(file.FullName);

                    //添加到资源文件中（资源名称中会将后缀名去掉）
                    rw.AddResource(Path.GetFileNameWithoutExtension(file.Name), img);
                }

                //执行生成
                rw.Generate();
                rw.Close();

                MessageBox.Show("处理完成。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch(Exception er)
            {
                MessageBox.Show(er.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCacnle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSelImage_Click(object sender, EventArgs e)
        {
            //选择图片资源所在目录
            FolderBrowserDialog dlgImageFolder = new FolderBrowserDialog();
            dlgImageFolder.Description = "请选择图片资源所在文件夹";
            dlgImageFolder.ShowNewFolderButton = false;
            if (DialogResult.OK == dlgImageFolder.ShowDialog(this))
                txtImagePath.Text = dlgImageFolder.SelectedPath;
        }

        private void btnSelFilePath_Click(object sender, EventArgs e)
        {
            //选择生成的资源文件保存目录
            SaveFileDialog dlgSaveFile = new SaveFileDialog();
            dlgSaveFile.DefaultExt = "resources";
            dlgSaveFile.FileName = "ResourcesFile4DotNet.resources";
            dlgSaveFile.Filter = "资源文件(*.resources)|*.resources";
            if (DialogResult.OK == dlgSaveFile.ShowDialog(this))
                txtResourcesFileName.Text = dlgSaveFile.FileName;
        }

        private void ResourcesFile4DotNetFrom_Load(object sender, EventArgs e)
        {
            //初始化加载时加载资源文件进行设置，可以视为调用的实例
        }
    }
}
